package com.ymatou.iapi.optrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.BizOperateLogEnum;
import com.ymatou.iapi.optrade.parameter.req.MarkOrderDeletedReq;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.MarkOrderDeletedCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderBizLogPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.constant.ECode;
/**
*删除订单
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_MarkOrderDeleted {
	
	private static MarkOrderDeletedReq markOrderDeletedReq;
	private static MarkOrderDeletedCall markorderdeletedCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除订单");
	}

	@Before
	public void caseUp() {
		markOrderDeletedReq = new MarkOrderDeletedReq();
		markorderdeletedCall = new MarkOrderDeletedCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(markorderdeletedCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verify() {

		long orderId = markOrderDeletedReq.getOrderId();
		OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);

		Logger.comment(String.format("校验订单表(order) orderId:%s", orderId));

		Logger.verifyEquals(true, orderPo.getDeleted(), "验证数据库OrderId");

		//订单主状态操作日志表(order_status_log)
		OrderBizLogPo orderBizLogPo = orderShardingWapper
				.selectOrderBizLogByOrderId(orderId);

		Logger.comment("验证订单业务操作记录表(order_biz_log)");

		String logContent = "原标记是:false 新标记:true";

		Logger.verifyEquals(logContent, orderBizLogPo.getContent(),
				"验证数据库Content");

		Logger.verifyEquals(orderId, orderBizLogPo.getOrderId(), "验证数据库OrderId");

		Logger.verifyEquals(String.valueOf(orderId), orderBizLogPo.getBizNo(),
				"验证数据库BizNo");
		Logger.verifyEquals(BizOperateLogEnum.DELETE_ORDER.toString(),
				orderBizLogPo.getBizType(), "验证数据库BizType");

		Logger.verifyEquals(markOrderDeletedReq.getUserId(),
				orderBizLogPo.getOperator(), "验证数据库Operator");

	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderDeleted_001() {
		Logger.start(true, "删除订单");
		try {
			//创建订单
			Logger.comment("前置数据准备开始");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();

			Logger.comment("前置数据准备结束");
			//参数
			markOrderDeletedReq.setDelete(true);
			markOrderDeletedReq.setOrderId(orderId);
			markOrderDeletedReq.setUserId(userId);
			//发送请求
			markorderdeletedCall.setData(markOrderDeletedReq);
			markorderdeletedCall.callService();

			Logger.verifyEquals(true, markorderdeletedCall.getSuccess(),
					"验证success");
			//数据库验证
			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderDeleted_002() {
		Logger.start(false, "订单号为空，删除订单失败");
		try {
			markOrderDeletedReq.setDelete(true);
			markOrderDeletedReq.setUserId(1113);
			//发送请求
			markorderdeletedCall.setData(markOrderDeletedReq);
			markorderdeletedCall.callService();

			Logger.verifyEquals(false, markorderdeletedCall.getSuccess(),
					"验证success");

			Logger.verifyIsContains(ECode.ERROR400_MSG_9,
					markorderdeletedCall.getErrorMessage(), "验证errorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderDeleted_003() {
		Logger.start(false, "用户Id不是当前订单的userId");
		try {
			//创建订单			
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			//参数
			markOrderDeletedReq.setDelete(true);
			markOrderDeletedReq.setOrderId(orderId);
			markOrderDeletedReq.setUserId(20324128);
			//发送请求
			markorderdeletedCall.setData(markOrderDeletedReq);
			markorderdeletedCall.callService();
			Logger.verifyEquals(false, markorderdeletedCall.getSuccess(),
					"验证success");
			Logger.verifyIsContains("当前用户不是订单" + orderId + "的所有者",
					markorderdeletedCall.getErrorMessage(), "验证errorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MarkOrderDeleted_005() {
		Logger.start(true, "订单状态为退款退货中,删除订单失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderApplyRefund();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();

			Logger.comment("前置数据准备结束");
			//参数
			markOrderDeletedReq.setDelete(true);
			markOrderDeletedReq.setOrderId(orderId);
			markOrderDeletedReq.setUserId(userId);
			//发送请求
			markorderdeletedCall.setData(markOrderDeletedReq);
			markorderdeletedCall.callService();

			Logger.verifyEquals(false, markorderdeletedCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("ERR_STATUS_NOTMATCH",
					markorderdeletedCall.getString("errorCode"), "验证errorCode");
			Logger.verifyEquals("哈尼，这个订单有一笔未完成的退货退款申请，建议完成后再删除哦~",
					markorderdeletedCall.getErrorMessage(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}